﻿Public Class _Default
    Inherits System.Web.UI.Page
    Dim name As String
    Dim con As SqlServerCe.SqlCeConnection = New SqlServerCe.SqlCeConnection("Data Source='c:\users\mauhib\documents\visual studio 2010\Projects\WebApplication5\WebApplication5\App_Data\MyData.sdf'")
    Dim sda As SqlServerCe.SqlCeDataAdapter
    Dim com As SqlServerCe.SqlCeCommand = New SqlServerCe.SqlCeCommand
    Dim dt As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            name = Session("User_Nick").ToString
            GetMsgs()
        Catch ex As Exception
            Response.Redirect("login.aspx")
        End Try
        Label1.Text = "Hello " + Session("User_Nick")
        End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        com.Connection = con
        com.CommandText = "Insert Into Chat (User_Nick, Time, Msg) values ('" + name + "','" + DateTime.Now + "', '" + WebTextEditor1.Text + "')"
        con.Open()
        com.ExecuteNonQuery()
        con.Close()
        WebTextEditor1.Text = ""
        GetMsgs()
    End Sub

    Private Sub GetMsgs()
        sda = New SqlServerCe.SqlCeDataAdapter("SELECT User_Nick, Msg, Time from Chat", con)
        dt.Clear()
        sda.Fill(dt)
        WebDataGrid1.DataSource = dt
    End Sub

    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
        Session.Remove("User_Nick")
        Response.Redirect("login.aspx")
    End Sub
End Class